#include <Windows.h>
#include <glut.h>
#include "floatimage.H"

static GLint    ImageWidth=500;
static GLint    ImageHeight=750;

floatimage reduced;
floatimage translucent;
float* reducedData;

GLuint displayID;

void averageAlphaPrime(float red, float green, float blue, float* result);
void maskOperation(int red, int green, int blue, int mask);

void init()
{
	glClearColor(0,0,0,0);
	gluOrtho2D(0,ImageWidth,0,ImageHeight);

	glGenTextures(1,&displayID);
	glBindTexture(GL_TEXTURE_2D, displayID);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, reduced.sizex, reduced.sizey, 0, GL_RGB, GL_FLOAT, reduced.buf);
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, ImageWidth, 0.0, ImageHeight);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glViewport(0, 0, ImageWidth, ImageHeight);

	glPolygonMode(GL_FRONT,GL_FILL);

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D,displayID);

	glBegin(GL_QUADS);
	glTexCoord2f(1.0, 1.0); 
	glVertex2f(0.0, 0.0);
	glTexCoord2f(0.0, 1.0); 
	glVertex2f(ImageWidth, 0.0);
	glTexCoord2f(0.0, 0.0); 
	glVertex2f(ImageWidth, ImageHeight);
	glTexCoord2f(1.0, 0.0); 
	glVertex2f(0.0, ImageHeight);
	glEnd();

	glDisable(GL_TEXTURE_2D);

	glutSwapBuffers();
}

void main(int argc, char *argv[])
{

	reduced.LoadPFM("reducedAlbedoDeep.pfm");
	translucent.LoadPFM("translucentDeep.pfm");

	

	/************************************************************************/
	/*						Mask Value                                      */
	/*				Red				Green			Blue					*/
	/*				255				241				235						*/
	/*				255				255				255						*/
	/*				252				221				220						*/
	/*				255				225				224						*/
	/*				255				237				232						*/
	/*				255				226				209						*/
	/************************************************************************/

	float result[3];

	averageAlphaPrime(1.27246,0.76318,0.64746,result);
	//averageAlphaPrime(255,255,255,result);
	//averageAlphaPrime(252,221,220,result);
	//averageAlphaPrime(255,225,224,result);	
	//averageAlphaPrime(255,237,232,result);
	//averageAlphaPrime(255,226,209,result);

	printf("%f\n%f\n%f\n",result[0],result[1],result[2]);

	//maskOperation(255,241,235,3);
	//maskOperation(255,255,255,1);
	//maskOperation(252,221,220,2);
	//maskOperation(255,225,224,5);	
	//maskOperation(255,237,232,4);
	//maskOperation(255,226,209,0);

	//reduced.SaveBMP("Mask.bmp");


	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(ImageWidth, ImageHeight);
	glutCreateWindow("Rasterization");
	init();
	glutDisplayFunc(display);
	glutMainLoop();
		
}

void averageAlphaPrime( float red, float green, float blue, float* result )
{
	int sum=0;
	result[0]=result[1]=result[2]=0;

	for(int x=0; x<reduced.sizex; x++)
	{
		for(int y=0; y<reduced.sizey; y++){

			float r,g,b;

			r=translucent.point(x,y).r;
			g=translucent.point(x,y).g;
			b=translucent.point(x,y).b;

			if (abs(r-red)<0.03&&abs(g-green)<0.03&&abs(b-blue)<0.03&&reduced.point(x,y).r>0.1)
			{
				sum++;
				result[0]+=reduced.point(x,y).r;
				result[1]+=reduced.point(x,y).g;
				result[2]+=reduced.point(x,y).b;
			}else{
				reduced.point(x,y).r=0;
				reduced.point(x,y).g=0;
				reduced.point(x,y).b=0;
			}
		}
	}

	result[0]/=sum;
	result[1]/=sum;
	result[2]/=sum;
}

void maskOperation( int red, int green, int blue, int mask )
{
	float factor=(float)(mask+1);
	factor=(factor/10);

	for(int x=0; x<reduced.sizex; x++)
	{
		for(int y=0; y<reduced.sizey; y++){

			int r,g,b;

			r=translucent.point(x,y).r*255;
			g=translucent.point(x,y).g*255;
			b=translucent.point(x,y).b*255;

			if (abs(r-red)<3&&abs(g-green)<3&&abs(b-blue)<3)
			{
				reduced.point(x,y).r=factor;
				reduced.point(x,y).g=factor;
				reduced.point(x,y).b=factor;
			}
		}
	}
}